import axios from 'axios';
import monitorTestData from 'data/monitorTestData';
import { makeAutoObservable } from 'mobx';

import StrorageUtils from 'utils/storageUtils'

/**
 * 汇总数据
 * 描述：监控汇总数据
 * 构造：
 */
var countData = {}
/**
 * 标签占比数据
 * 描述：标签的衍生标签的占比情况
 * 构造：
 */
var tagRatioData =  []
/**
 * 活动排名数据
 * 描述：活动按参与人数排名
 * 构造：
 */
var activityRankData =  []
/**
 * 标签排名数据
 * 描述：标签按参与人数排名
 * 构造：
 */
var tagRankData =  []
/**
 * 活动数据
 * 描述：活动每月数据
 * 构造：
 */
var activityFlowData  = []

var lastJoinRate = 0
/**
 * 营销store
 * 描述：营销监控数据存储
 * 调用：营销效果中心
 * 时间：20220404
 */
class MarketingStore {
    constructor() {
        makeAutoObservable(this)
    } 

    //展示的数据的起始位置
    showIndex =  0
    

    apiCountData = async(isUpdate) => {
        
         axios.get(axios.defaults.baseURL + `/api/monitor/count`).then(
             response => {
                 
                 countData = response.data
                 if (StrorageUtils.getCrowds()['crowds'] && StrorageUtils.getCrowds()['crowds'] !== countData['totalCrowd']){
                    countData['joinRate'] = countData['totalCrowd'] - StrorageUtils.getCrowds()['crowds'] 
                    StrorageUtils.saveCrowds({'crowds':countData['totalCrowd'],'joinRate':countData['joinRate']})
                 
                } else {
                    countData['joinRate'] = StrorageUtils.getCrowds()['joinRate'] || 0
                 }
                 
                 

                 if(isUpdate){
                     this.showIndex = this.showIndex + 1
                 }
             },
             error => {
                 console.log('ERROR',error)
                 countData = monitorTestData.countTestData
        
             }
         )
 
     }
    apiactivityFlowData = async(isUpdate) => {
        
        
         axios.get(axios.defaults.baseURL + `/api/monitor/activity/flow`).then(
             response => {
                 activityFlowData = response.data
                 if(isUpdate){
                    this.showIndex = this.showIndex + 1
                }
             },
             error => {
                 console.log('ERROR',error)
                 activityFlowData = monitorTestData.activityFlowTestData
                 
             }
         )
 
     }

    apiActivityRankData = async(isUpdate) => {
        
        
         axios.get(axios.defaults.baseURL + `/api/monitor/activity/rank`).then(
             response => {
                 activityRankData = response.data
                 if(isUpdate){
                    this.showIndex = this.showIndex + 1
                }
             },
             error => {
                 console.log('ERROR',error)
                 activityRankData = monitorTestData.activityRankTestData
                 
             }
         )
 
     }

    apiTagRankData = async(isUpdate) => {
        
        
         axios.get(axios.defaults.baseURL + `/api/monitor/tag/rank`).then(
             response => {
                tagRankData = response.data
                if(isUpdate){
                    this.showIndex = this.showIndex + 1
                }
             },
             error => {
                 console.log('ERROR',error)
                 tagRankData = monitorTestData.tagRankTestData
                 
             }
         )
 
     }

    apiTagRatioData = async(isUpdate) => {
        
        
         axios.get(axios.defaults.baseURL + `/api/monitor/tag/ratio`).then(
             response => {
                 tagRatioData = response.data
                 tagRatioData.map((tagRatio,index)=>{
                    tagRatio.ratio.map(ratioItem=>ratioItem.value=parseInt(ratioItem.value))
                })
                 
                 if(isUpdate){
                    this.showIndex = this.showIndex + 1
                }
             },
             error => {
                 console.log('ERROR',error)
                 tagRatioData = monitorTestData.tagRatioTestData
             }
         )
 
     }

    /**
     * 更改展示数据的起始index
     * @param {*} index 
     */
    setShowIndex(index){
        this.showIndex = index
    }
    
    getCountData(){
        return countData;
    }
    getTagRatioData(){
        return tagRatioData
    }
    getActivityRankData(){
        return activityRankData
    }
    getTagRankData(){
        return tagRankData
    }
    getActivityFlowData(){
        return activityFlowData
    }


}
const marketingStore = new MarketingStore()
export default marketingStore